const app = getApp();
import {
  post
} from '../../utils/request';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabsFixed: false,
    listResult: {
      list: [],
      total: 0
    },
    listPayload: {
      pageNum: 1,
      pageSize: 10,
      param: {
        status: -1
      },
      loading: false,
      hasMore: true,
    }
  },

  // 订单详情
  toDetailPage(e) {
    let {
      orderno
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/order/detail?orderNo=${orderno}`,
    });
  },

  // 状态改变
  onChgStatus(e) {
    let {
      status
    } = e.currentTarget.dataset;
    this.refreshList(status);
  },

  // 获取订单列表
  getList() {
    let that = this;
    if (that.data.listPayload.loading || (!that.data.listPayload.hasMore)) {
      return;
    }
    app.checkLogin(true, () => {
      that.setData({
        listPayload: {
          ...that.data.listPayload,
          loading: true
        }
      });
      wx.showNavigationBarLoading();
      post("/order/list-by-user", that.data.listPayload)
        .then(resp => {
          wx.stopPullDownRefresh()
          wx.hideNavigationBarLoading();
          let hasMore = true;
          if (0 == resp.code) {
            let listResult = that.data.listResult;
            let listPayload = that.data.listPayload;
            let list = (listPayload.pageNum === 1) ? resp.data.list : [...listResult.list, ...resp.data.list];
            list = list.map(d => {
              if (d.status === 0) {
                if (d.cancelStatus === 1) {
                  d.status = -1; // 已取消
                } else if (d.expireStatus === 1) {
                  d.status = -2; // 已超时
                }
              }
              if (d.status === 1 || d.status === 2) {
                if (d.refundStatus === 1) {
                  d.status = -3; // 退款中
                } else if (d.refundStatus === 2) {
                  d.status = -4; // 已退款
                }
              }
              return d;
            });
            if (resp.data.list.length < listPayload.pageSize) {
              hasMore = false;
            }
            that.setData({
              listPayload: {
                ...listPayload,
                loading: false,
                hasMore
              },
              listResult: {
                total: resp.data.total,
                list
              }
            });
          } else {
            that.setData({
              listPayload: {
                ...listPayload,
                loading: false
              }
            });
          }
        })
    });
  },

  // 刷新列表
  refreshList(status) {
    this.setData({
      listResult: {
        list: [],
        total: 0
      },
      listPayload: {
        pageNum: 1,
        pageSize: 10,
        param: {
          status
        },
        loading: false,
        hasMore: true,
      }
    });
    this.getList();
  },

  // 删除订单
  del(e) {
    let that = this;
    let {
      orderno
    } = e.currentTarget.dataset;

    wx.showModal({
      title: '提示',
      content: '您是否确定删除此订单',
      complete: (res) => {
        if (res.confirm) {
          wx.showLoading({
            title: '正在删除',
            mask: true
          });
          post(`/order/del-by-no/${orderno}`)
            .then(res => {
              wx.hideLoading();
              if (res.code === 0) {
                that.refreshList();
              }
              if (res.code === -1) {
                wx.showToast({
                  title: res.msg,
                });
              }
            });
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let {
      status
    } = options;
    if (undefined === status) {
      status = -1;
    }
    this.refreshList(parseInt(status));
    wx.setBackgroundColor({
      backgroundColor: "#f6f6f6"
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.refreshList(this.data.listPayload.param.status);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.listPayload.loading || (!this.data.listPayload.hasMore)) {
      return;
    }
    this.setData({
      listPayload: {
        ...this.data.listPayload,
        pageNum: this.data.listPayload.pageNum + 1
      }
    });
    this.getList();
  },

  onPageScroll(e) {
    let {
      tabsFixed
    } = this.data;
    if (e.scrollTop > 0) {
      if (!tabsFixed) {
        this.setData({
          tabsFixed: true
        });
      }
    } else {
      if (tabsFixed) {
        this.setData({
          tabsFixed: false
        });
      }
    }
  },

});